﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Core;

namespace DIaLOGIKa.b2xtranslator.ZipUtils
{
    class SharpZipWriter : ZipWriter
    {
        protected ZipFile _zipFile;
        protected String _lastName;
        protected IDictionary<String, DocxDataSource> _zipData;

        internal SharpZipWriter(Stream stream)
        {
            _zipFile = new ZipFile(stream);
            _zipData = new Dictionary<String, DocxDataSource>();
		}
	    
	    public override void AddEntry(string relativePath)
        {
            _lastName = ZipLib.ResolvePath(relativePath);
            _zipData.Add(_lastName, new DocxDataSource());
	    }	    	
	    
	    public override void Close()
        {
            if (_zipFile.Count != 0)
            {
                return;
            }
            _zipFile.BeginUpdate();
            foreach (String s in _zipData.Keys)
            {
                _zipFile.Add(_zipData[s], s);
            }
            _zipFile.CommitUpdate();
        }
	    
	    public override int Read(byte[] buffer, int offset, int count)
		{
            throw new ZipException("Error, Read not allowed on this stream");
		}

		public override void Write(byte[] buffer, int offset, int count)
		{
            if (_zipData.ContainsKey(_lastName))
            {
                if (_zipData[_lastName].Count == 0)
                {
                    if (_lastName.Contains("[Content_Types].xml") || _lastName.Contains("document.xml.rels")
                   || _lastName.Contains(".rels"))
                    {
                        _zipData[_lastName].DiscardNullValue = true;
                    }
                    _zipData[_lastName].Append(buffer);
                }
            }
        }
        public override long Seek(long offset, SeekOrigin origin)
        {
            return 0;
        }

        public override void SetLength(long value)
        {
            return;
        }

        public override void Flush()
        {
            return;
        }

        public override long Position
        {
            get
            {
                throw new ZipException("Position not available on this stream");
            }
            set
            {
                throw new ZipException("Position not available on this stream");
            }
        }

        public override long Length
        {
            get
            {
                return 0;
            }
        }

        public override bool CanRead
        {
            get
            {
                return false;
            }
        }

        public override bool CanWrite
        {
            get
            {
                return true;
            }
        }

        public override bool CanSeek
        {
            get
            {
                return false;
            }
        }
       
    }
}
